package com.control.servlet;

import com.dao.UserDao;
import com.model.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class loginServlet extends HttpServlet {

    UserDao userDao = new UserDao();

    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        HttpSession session = req.getSession();

        if(action==null||action.length()<=0){
            User user = userDao.login(req.getParameter("name"),req.getParameter("password"));
            if(user==null){
                String tip="用户名或者密码错误，请重新登录！";
                req.setAttribute("tip", tip);
                RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/login.jsp");
                dispatcher.forward(req, resp);
            }
            else{
                if(user.getStatus().equals("0")){
                    String tip="此账号已停封";
                    req.setAttribute("tip", tip);
                    RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/login.jsp");
                    dispatcher.forward(req, resp);
                }
                else {
                    session.setAttribute("user",user);
                    resp.sendRedirect("/store/jsp/home.jsp");
                }
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        HttpSession session = req.getSession();

        if (action.equals("qq")) {
            String openId = (String) req.getAttribute("openId");
            User user = userDao.loginByQQ(openId);
            if (user == null) {
                req.setAttribute("openId", openId);
                req.setAttribute("tip","请注册新账号以绑定该QQ");
                RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/register.jsp");
                dispatcher.forward(req, resp);
            } else {
                session.setAttribute("user", user);
                resp.sendRedirect("/store/jsp/home.jsp");
            }
        }
        else if (action.equals("fromRegister")) {
            String name = (String) req.getAttribute("name");
            String password = (String) req.getAttribute("password");
            User user = userDao.login(name,password);
            if (user == null) {
                req.setAttribute("tip","注册失败");
                RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/register.jsp");
                dispatcher.forward(req, resp);
            } else {
                session.setAttribute("user", user);
                resp.sendRedirect("/store/jsp/home.jsp");
            }
        }
        else if(action.equals("logout")){
            session.removeAttribute("user");
            resp.sendRedirect("/store/jsp/home.jsp");
        }
    }
}
